package com.yl.order.ui.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.yl.common.ui.BaseDialogFragment;
import com.yl.order.R;
import com.yl.order.interfaces.Callback;


/**
 * create by cjx on 2024/5/16
 * desc: 确认对话框
 */
public class ConfirmDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private Callback<Integer> confirmCallback;
    private String title;
    private String describe;
    private int confirmBgRes;
    private int cancelBgRes;

    private String cancelText;
    private String confirmText;
    private Callback<Integer> cancelCallback;

    public static ConfirmDialogFragment newInstance() {
        return new ConfirmDialogFragment();
    }

    @Override
    public int getViewId() {
        return R.layout.dialog_confirm;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!TextUtils.isEmpty(title)) {
            ((TextView) findViewById(R.id.title)).setText(title);
        }
        ((TextView) findViewById(R.id.describe)).setText(describe);
        TextView cancelView = findViewById(R.id.cancel);
        TextView confirmView = findViewById(R.id.confirm);
        if (confirmBgRes > 0) {
            confirmView.setBackgroundResource(confirmBgRes);
        }
        if(cancelBgRes > 0){
            cancelView.setBackgroundResource(cancelBgRes);
        }
        if (!TextUtils.isEmpty(cancelText)) {
            cancelView.setText(cancelText);
        }
        if (!TextUtils.isEmpty(confirmText)) {
            confirmView.setText(confirmText);
        }
        findViewById(R.id.close).setOnClickListener(this);
        findViewById(R.id.confirm).setOnClickListener(this);
        cancelView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        dismiss();
        if (id == R.id.confirm) {
            if (confirmCallback != null) {
                confirmCallback.callback(0);
            }
        } else if (id == R.id.cancel && cancelCallback != null) {
            cancelCallback.callback(0);
        }
    }

    public ConfirmDialogFragment setCanceled(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public ConfirmDialogFragment setConfirmCallback(Callback<Integer> confirmCallback) {
        this.confirmCallback = confirmCallback;
        return this;
    }

    public ConfirmDialogFragment setTitle(String title) {
        this.title = title;
        return this;
    }

    public ConfirmDialogFragment setDescribe(String describe) {
        this.describe = describe;
        return this;
    }

    public ConfirmDialogFragment setConfirmBackground(int res) {
        this.confirmBgRes = res;
        return this;
    }

    public ConfirmDialogFragment setCancelBackground(int res) {
        this.cancelBgRes = res;
        return this;
    }

    public ConfirmDialogFragment setConfirmButton(String text, Callback<Integer> confirmCallback) {
        this.confirmText = text;
        this.confirmCallback = confirmCallback;
        return this;
    }

    public ConfirmDialogFragment setCancelButton(String text, Callback<Integer> cancelCallback) {
        this.cancelText = text;
        this.cancelCallback = cancelCallback;
        return this;
    }
}
